package cz.zcu.fav.ratdroid.activity.calendar.reservation;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import cz.zcu.fav.ratdroid.R;
import cz.zcu.fav.ratdroid.activity.calendar.CalendarActivity;
import cz.zcu.fav.ratdroid.bean.Period;
import cz.zcu.fav.ratdroid.bean.Reservation;
import cz.zcu.fav.ratdroid.service.RestService;

public class EditReservationActivity extends AbstractReservationActivity
{
	private Period period;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		period = (Period) getIntent().getSerializableExtra(
				Period.SINGLE_PERIOD_IDENTIFIER);

		super.onCreate(savedInstanceState);

		getNumberOfWeeksButton().setEnabled(false);
		getPeriodicalSwitch().setEnabled(false);
	}

	@Override
	public Period loadBackgroudData()
	{
		return ReservationFactory.clonePeriod(period);
	}

	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.reservation_edit_menu, menu);
		if (!period.getReservation().isPeriodical())
		{
			menu.removeItem(R.id.reservationPeriodicalButton);
		}
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case R.id.reservationPeriodicalButton:
			Intent obj = new Intent(this, ReservationPeriodsActivity.class);
			obj.putExtra(Reservation.SINGLE_RESERVATION_IDENTIFIER,
					period.getReservation());
			startActivityForResult(obj,
					CalendarActivity.INVALIDATE_CALENDAR_RESULT_KEY);
			break;
		case R.id.reservationSubmitButton:
			if (validateForm())
			{
				Period per = getModifiedPeriod();
				if (!per.equals(period))
				{
					setRestTask(RestService.getInstance().modifyPeriod(this,
							per.getId(), per.isValid(), per.getFromDate(),
							per.getToDate(), this));
				}
			}
			break;
		case R.id.reservationDeleteButton:

			new AlertDialog.Builder(this)
					.setIcon(android.R.drawable.ic_dialog_alert)
					.setTitle(getString(R.string.deleteDialogHead))
					.setMessage(getString(R.string.deleteDialogText))
					.setPositiveButton(getString(R.string.deleteDialogYes),
							new DialogInterface.OnClickListener()
							{

								public void onClick(DialogInterface dialog,
										int which)
								{
									setRestTask(RestService
											.getInstance()
											.deleteReservation(
													period.getReservation(),
													EditReservationActivity.this,
													EditReservationActivity.this));

								}

							}).setNegativeButton(getString(R.string.deleteDialogNo), null).show();
			break;
		default:
			return true;
		}
		return true;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (requestCode == CalendarActivity.INVALIDATE_CALENDAR_RESULT_KEY)
		{
			if (resultCode == android.app.Activity.RESULT_OK)
			{
				Intent returnIntent = new Intent();
				setResult(RESULT_OK, returnIntent);
				finish();
			}
		}
	}

}
